import unittest
from sirogi.letter.models import Letter
from sirogi.documents.models import Document
from django.test.client import Client


class BasicTestCase(unittest.TestCase):
     #def setUp(self):
     #   us = SirogiUser.objects.create()
      #  self.let1 = Letter.objects.create(Recipient =  us, Sender = "sender", Subject = "subject", Body = "body",  Read = False)
       
    
    #def test_read_letter(self):
        #self.assertEquals(self.let1.Body, 'body')
    def test_find_documents_by_name(self):
        c = Client()
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.post('/approve/',{'user': '-1','document_nro': '' })        
    	self.failUnlessEqual(response.status_code, 200)
    	
    	#Cuento la cantidad de documentos
    
    	d = Document.objects.all()
        self.failUnlessEqual(response.content.count('/approve/approve'), d.filter(state__Name = 'E').count())
    	

 #=============================================================================
    def test_enter_approve_a_document_unauthorized_user(self):
        c = Client()
        
        #Sin un usuario logueado
        #llamo a la pagina
        response = c.get('/approve/')
        #302 me redirecciona a la pagina de login.
        self.failUnlessEqual(response.status_code, 302)
        
        #Con un usuario Editor logueado
        self.failUnlessEqual(c.login(username='editor', password='editor'), True)
        response = c.get('/approve/')
        self.failUnlessEqual(response.status_code, 302)
        
        #Con un usuario Guest logueado
        c.logout()
        self.failUnlessEqual(c.login(username='guest', password='guest'), True)
        response = c.get('/approve/')
        self.failUnlessEqual(response.status_code, 302)

 #=============================================================================
    def test_enter_approve_a_document_authorized_user(self):
        c = Client()
        #Verifico que se pueda acceder con un usuario inspector
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.get('/approve/')
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual("Approve list" in response.content, True)
        
        #verifico que se pueda acceder con un usuario manager
        c.logout()
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.get('/approve/')
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual("Approve list" in response.content, True)
        

 #=============================================================================        
    def test_approve_a_document_unauthorized_user(self):
        c = Client()
        
        #Sin un usuario logueado
        #llamo a la pagina
        response = c.get('/approve/approve/2/')
        #301 = HttpResponsePermanentRedirec
        self.failUnlessEqual(response.status_code, 302)
                
        #Con un usuario Editor logueado
        self.failUnlessEqual(c.login(username='editor', password='editor'), True)
        response = c.get('/approve/approve/2/')
        #301 = HttpResponsePermanentRedirec
        self.failUnlessEqual(response.status_code, 302)
        
        #Con un usuario Guest logueado
        c.logout()
        self.failUnlessEqual(c.login(username='guest', password='guest'), True)
        response = c.get('/approve/approve/2/')
        self.failUnlessEqual(response.status_code, 302)


 #=============================================================================        
    def test_approve_a_document_authorized_user(self):
        c = Client()
        #Verifico que se pueda acceder con un usuario inspector
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.get('/approve/approve/2/')
        self.failUnlessEqual(response.status_code, 200)

        self.failUnlessEqual("The document was approved" in response.content, True)

        #verifico que el documento no este mas en la lista de aprovados
        self.failUnlessEqual("/approve/approve/2" in response.content, False)
        
        #verifico que se pueda acceder con un usuario manager
        c.logout()
        self.failUnlessEqual(c.login(username='manager', password='manager'), True)
        response = c.get('/approve/approve/3/')
        self.failUnlessEqual(response.status_code, 200)
        
        self.failUnlessEqual("The document was approved" in response.content, True)
        self.failUnlessEqual("/approve/approve/3" in response.content, False)
        

 #=============================================================================        
    def test_disapprove_a_document_unauthorized_user(self):
        c = Client()
        
        #Sin un usuario logueado
        #llamo a la pagina
        response = c.get('/approve/disapprove/4/')
        #301 = HttpResponsePermanentRedirec
        self.failUnlessEqual(response.status_code, 302)
        
        #Con un usuario Editor logueado
        self.failUnlessEqual(c.login(username='editor', password='editor'), True)
        response = c.get('/approve/disapprove/4/')
        #301 = HttpResponsePermanentRedirec
        self.failUnlessEqual(response.status_code, 302)
        
        #Con un usuario Guest logueado
        c.logout()
        self.failUnlessEqual(c.login(username='guest', password='guest'), True)
        response = c.get('/approve/disapprove/4/')
        self.failUnlessEqual(response.status_code, 302)

 #=============================================================================        
    def test_disapprove_a_document_authorized_user(self):
        c = Client()
        #Verifico que se pueda acceder con un usuario inspector
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.get('/approve/disapprove/4/')
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual("The document was disapproved" in response.content, True)
        #verifico que el documento no este mas en la lista de aprovados
        self.failUnlessEqual("/approve/disapprove/4" in response.content, False)
        
        #verifico que se pueda acceder con un usuario manager
        c.logout()
        self.failUnlessEqual(c.login(username='manager', password='manager'), True)
        response = c.get('/approve/disapprove/5/')
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual("The document was disapproved" in response.content, True)
        self.failUnlessEqual("/approve/disapprove/5" in response.content, False)
 
 
 #============================================================================= 
    def test_edit_a_document_unauthorized_user(self):
        c = Client()
        
        #Sin un usuario logueado
        #llamo a la pagina
        response = c.get('/approve/edit/6/')
        #302 = HttpResponseRedirect
        self.failUnlessEqual(response.status_code, 302)
        
        #Con un usuario Editor logueado
        self.failUnlessEqual(c.login(username='editor', password='editor'), True)
        response = c.get('/approve/edit/6/')
        #302 = HttpResponseRedirect
        self.failUnlessEqual(response.status_code, 302)
                
        #Con un usuario Guest logueado
        c.logout()
        self.failUnlessEqual(c.login(username='guest', password='guest'), True)
        response = c.get('/approve/edit/6/')
        #302 = HttpResponseRedirect
        self.failUnlessEqual(response.status_code, 302)

 #=============================================================================        
    def test_edit_a_document_authorized_user(self):
        c = Client()
        
        #Verifico que se pueda acceder con un usuario inspector
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.get('/approve/edit/6/')
        self.failUnlessEqual(response.status_code, 200)

        self.failUnlessEqual("The document was marked" in response.content, True)
        #verifico que el documento no este mas en la lista de aprovados
        self.failUnlessEqual("/approve/edit/6" in response.content, False)
        
        #verifico que se pueda acceder con un usuario manager
        c.logout()
        self.failUnlessEqual(c.login(username='manager', password='manager'), True)
        response = c.get('/approve/edit/7/')
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual("The document was marked" in response.content, True)
        self.failUnlessEqual("/approve/edit/7" in response.content, False)

 #=============================================================================
    def test_unexist_a_document_authorized_user(self):
        c = Client()
        
        #Verifico que se pueda acceder con un usuario inspector
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.get('/approve/edit/1777/')
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual("The document does not exist" in response.content, True)
   
 #=============================================================================   
    def test_not_sent_revision_document_authorized_user(self):
        c = Client()
        
        #Verifico que se pueda acceder con un usuario inspector
        self.failUnlessEqual(c.login(username='inspector', password='inspector'), True)
        response = c.get('/approve/edit/8/')
        self.failUnlessEqual(response.status_code, 200)
        self.failUnlessEqual("Error in the transaction." in response.content, True)

